<?php

class SF_Ulmart_Block_List extends Mage_Core_Block_Template {
	const DEFAULT_CATEGORY_ID = '4';
	protected $categoryHtml = '';
	protected $pagerHtml 	= '';
	protected $pageSize     = '50';
	protected $curPage;
	protected $categoryId;
	protected $total;
	
	
	public function getCollection(){
		$fielder = $this->getRequest()->getParam('fielder');
		$this->curPage  = $this->getRequest()->getParam('page') ? $this->getRequest()->getParam('page') : 1;
		$this->categoryId = $this->getRequest()->getParam('category_list')
							? $this->getRequest()->getParam('category_list')
							: self::DEFAULT_CATEGORY_ID;

		$sku_id = htmlspecialchars(trim($this->getRequest()->getParam('sku_id')));
		$startTime = htmlspecialchars(trim($this->getRequest()->getParam('startTime')));
		$endTime = htmlspecialchars(trim($this->getRequest()->getParam('endTime')));
		

		$category = Mage::getModel('catalog/category')->load($this->categoryId);
		$productCollection = $category->getProductCollection()
				 ->addAttributeToSelect('*')
				 ->addAttributeToFilter('visibility', '4')
				 ->addAttributeToFilter('approval', '2')
				 ->addAttributeToFilter('status', '1')
				 ->setPageSize($this->pageSize)
				 ->setCurPage($this->curPage)
				 ->setOrder('entity_id','desc');

		if($sku_id){
			if(is_numeric($sku_id)){
				$productCollection->addAttributeToFilter('entity_id', array('like' => $sku_id));
			}else{
				$productCollection->addAttributeToFilter('sku', array('like' => $sku_id));
			}
		}
		
		if($startTime && $endTime){
			$productCollection->addAttributeToFilter('created_at', array('from' => $startTime,'to' => $endTime));
		}elseif ($startTime) {
			$productCollection->addAttributeToFilter('created_at', array('gteq' => $startTime));
		}elseif($endTime){
			$productCollection->addAttributeToFilter('created_at', array('lteq' => $endTime));
		}   

		switch($fielder){
			case '0':
					$productCollection->getSelect()
					->joinLeft(
						array('rup'=>'rumall_ulmart_product'),
						'e.entity_id = rup.product_id',
						array('*'));
					break;
			case '1':
					$productCollection->getSelect()
					->joinLeft(
						array('rup'=>'rumall_ulmart_product'),
						'e.entity_id = rup.product_id',
						array('*'))
					->where('rup.is_selected = 1');
					break;
			case '2':
					$productCollection->getSelect()
					->joinLeft(
						array('rup'=>'rumall_ulmart_product'),
						'e.entity_id = rup.product_id',
						array('*'))
					->where('rup.is_selected = 2');
					break;
			case '3':
					$productCollection->getSelect()
					->joinLeft(
						array('rup'=>'rumall_ulmart_product'),
						'e.entity_id = rup.product_id',
						array('*'))
					->where('rup.is_selected is null');
					break;
			case '4':
					$productCollection->getSelect()
					->joinLeft(
						array('rup'=>'rumall_ulmart_product'),
						'e.entity_id = rup.product_id',
						array('*'))
					->where('rup.is_confirm = 1');
					break;
			case '5':
					$productCollection->getSelect()
					->joinLeft(
						array('rup'=>'rumall_ulmart_product'),
						'e.entity_id = rup.product_id',
						array('*'))
					->where('rup.is_confirm = 2');
					break;
			default:
					$productCollection->getSelect()
					->joinLeft(
						array('rup'=>'rumall_ulmart_product'),
						'e.entity_id = rup.product_id',
						array('*'));
					break;
		}
		$this->total = $productCollection->getSize();
		$this->setPager($productCollection->getSize(),$this->pageSize);
		
		return $productCollection;
		
	}
	

	protected function _tocategoryHtml(){
        return parent::_tocategoryHtml();
    }
	
	public function setPager($total,$size){
		$totalPage = ceil($total / $size);
		$this->pagerHtml .= '<select name="page">';
		for($i=1;$i<=$totalPage;$i++){
			if($i == $this->curPage){
				$this->pagerHtml .= '<option value="'.$i.'" selected="selected">';
			}else{
				$this->pagerHtml .= '<option value="'.$i.'">';
			}
			$this->pagerHtml .= $i;
			$this->pagerHtml .= '</option>';
		}
		$this->pagerHtml .= '</select>';
	}
	
	public function getPager(){
		return $this->pagerHtml;
	}
	
	public function getFielder(){
		$curFielder = $this->getRequest()->getParam('fielder');
		$curFielder = !empty($curFielder) ? $curFielder : 0 ;
		$arr = array(
						'0'=>'',
						'1'=>'Yes',
						'2'=>'No',
						'3'=>'Null',
						'4'=>'Not uploaded',
						'5'=>'Uploaded',
					);
		$html = '';
		$html .= '<select name="fielder">';
		foreach($arr as $key=>$val){
			if($key == $curFielder){
				$html .= '<option value="'.$key.'" selected="selected">';
			}else{
				$html .= '<option value="'.$key.'">';
			}
			$html .= $this->__($val);
			$html .= '</option>';
		}
		$html .= '</select>';
		return $html;
	}
	
	public function getTreeCategories($parentId){
		$allCats = Mage::getModel('catalog/category')->getCollection()
					->addAttributeToSelect('*')
					->addAttributeToFilter('is_active','1')
					->addAttributeToFilter('level',array('in'=>array('2','3')));
		$this->categoryHtml .= '<select name="category_list">';
		$this->getNodeTree($allCats);
		$this->categoryHtml .= '</select>';
		return $this->categoryHtml;
	}

	private function getNodeTree($data)
	{
	
		$topNodes = $this->getTopNodes($data);
		foreach ($topNodes as $index => $node)
		{
			$topNodes[$index] = $this->getChildNodes($node, $data);
		}
		return $topNodes;
	}
	
	private function getTopNodes($data)
	{
		$ids = array();
		foreach ($data as $row) {
			$ids[] = $row->getData('entity_id');
		}
		
		$topNodes = array();
		foreach ($data as $row)
		{
			if (!in_array($row->getData('parent_id'), $ids)) {
				$topNodes[] = $row;
			}
		}
		return $topNodes;
	}
	
	private function getChildNodes($node, $data)
	{
		if($this->categoryId == $node->getId()){
			$this->categoryHtml .= '<option value="'.$node->getId().'" selected="selected">';
		}else{
			$this->categoryHtml .= '<option value="'.$node->getId().'">';
		}
		$this->categoryHtml .= $node->getName();
		$this->categoryHtml .= '</option>';
			
		foreach ($data as $row)
		{
			if ($node->getData('entity_id') == $row->getData('parent_id'))
			{
				if($this->categoryId == $row->getId()){
					$this->categoryHtml .= '<option value="'.$row->getId().'" selected="selected">';
				}else{
					$this->categoryHtml .= '<option value="'.$row->getId().'">';
				}
				$this->categoryHtml .= ' ---- '. $row->getName();
				$this->categoryHtml .= '</option>';
			}
		}
	}
	
}
